package cn.study.controller;


import cn.study.entity.StudyBanner;
import cn.study.entity.StudyExamination;
import cn.study.service.StudyExaminationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 考试表 前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-06-22
 */
@Controller
@Slf4j
@RequestMapping("/study-examination")
public class StudyExaminationController {
    @Resource
    StudyExaminationService examinationService;
    @GetMapping("/list")
    public String list(Model model, @RequestParam(value="pn",defaultValue="1") Integer pn,
                       @RequestParam(value="paginalsize",defaultValue = "5") Integer paginalsize)
    {
        PageHelper.startPage(pn, paginalsize);

        List<StudyExamination> list=examinationService.getlist();
        PageInfo pInfo=new PageInfo(list,10);
        model.addAttribute("pInfo",pInfo);
        model.addAttribute("paginalsize",paginalsize);

        return "study_examination";
    }
    @GetMapping("/add")
    public String add()
    {

        return "study_examination_add";
    }

    @PostMapping("/examinationadd")
    public String examinationadd(StudyExamination examination,
                                String examinationBeginTime1,  String examinationEndTime1) throws ParseException {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        examination.setExaminationBeginTime(sdf.parse(examinationBeginTime1));
        examination.setExaminationEndTime(sdf.parse(examinationEndTime1));
        boolean bl=examinationService.save(examination);


        if(bl){
            log.info("添加成功");
        }else {
            log.info("添加失败");
        }
        return "redirect:/study-examination/list";
    }
    @GetMapping("del")
    public String del(Integer id)
    {
        boolean bl=examinationService.removeById(id);
        if(bl){
            log.info("删除成功");
        }else {
            log.info("删除失败");
        }
        return "redirect:/study-examination/list";
    }
    @GetMapping("/upd")
    public String upd(Integer id,Model model)
    {
        StudyExamination studyExamination=examinationService.getById(id);
        model.addAttribute("list",studyExamination);


        return "study_examination_upd";
    }

    @PostMapping("/examinationupd")
    public String examinationupd(Model model,StudyExamination studyExamination,String examinationBeginTime1,  String examinationEndTime1) throws ParseException
    {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        studyExamination.setExaminationBeginTime(sdf.parse(examinationBeginTime1));
        studyExamination.setExaminationEndTime(sdf.parse(examinationEndTime1));
        examinationService.updateById(studyExamination);
        return "redirect:/study-examination/list";
    }
}

